<template>
  <div class="wrap">
    <template>
      <div class="parametric-select" v-if="!loading">
        <show-table :isEmptyColNames="isEmptyColNames"
                    :showTab="false"
                    ref="showTable"
                    :permitRemove="false"
                    :datum="datum"
                    :fill-mode="true"
                    :permitAdd="true"
                    headerAlign="center"
                    selected-mode="cell"
                    :excelImport="false"
                    :show-operation-btn="true"
                    :enable-custom-scroll="true"
                    :getSelectRow="getSelectRow"
                    :cellValidate="checkContentIsNumber"
                    :editable="true"
                    width="100%"/>
      </div>
    </template>
    <div class="btn">
<!--      <el-button type="primary" :disabled="loading" @click="showRefPicture = !showRefPicture">{{-->
<!--          refPictureBtnText-->
<!--        }}-->
<!--      </el-button>-->
      <el-button type="primary" :disabled="loading" @click="insert">{{ $t('message.ElementLibrary.insert') }}
      </el-button>

      <el-button
        size="small"
        @click="close">
        {{ $t('message.WingProfileDesignDlg.cancel') }}
      </el-button>
    </div>
  </div>
</template>

<script>
  import DialogMixin from "../DialogMixin";
  import {elementLibraryCmd} from "../../../Command/ElementLibrary/ElementLibraryCommand";
  import {language} from "../../../util/Language";
  import ShowTable from "../../BaseComponents/ShowTable.vue"

  export default {
    components: {ShowTable},
    mixins: [DialogMixin],
    name: "SelectParameterInsertDlg",
    props: ["insertParams"],
    data() {
      return {
        datum: [],
        isEmptyColNames: [],
        selectRow: -1,
        loading: true,
      }
    },
    methods: {
      changePluginContainerHeight() {
        elementLibraryCmd.changePluginContainerHeight({minWidth: this.$el.clientWidth, maxHeight: this.$el.clientHeight})
      },
      close() {
        elementLibraryCmd.closeActiveCmd();
      },
      getSelectRow(row) {
        this.selectRow = row;
      },
      checkContentIsNumber(content) {
        let result = {
          validateResult: true,
          message: ''
        };
        let isNumberRegx = /^\d*(\.)*\d*$/;
        let isNumber = isNumberRegx.test(content);
        if (!isNumber) {
          result.validateResult = false;
          result.message = language.getMessage("message.HoleCmd.inputNumber");
        }

        return result;
      },
      fillData(parameters) {
        let org = parameters;
        this.datum = org?.map(item => {
          let param = {};
          item.variableParameters.forEach(cur => {
            param[cur.name] = cur.expression;
          });
          return param;
        });
        this.isEmptyColNames = Object.keys(this.datum ? this.datum[0] : []);
        //this.$refs.showTable.setCurrent(0);
        this.loading = false;
      },

      //参数化模型进行插入
      insert() {
        //获取当前选取的模型的模板,可能因为两个vue界面进行交互，ElementLibraryManager会new两次，导致数据丢失
        //let curElementTemplate = elementLibraryManager.getItemById(this.templateParams.id);
        let curElementTemplate = this.templateParams;

        let parametricParam = [];
        let keys = Object.keys(this.datum[this.selectRow]);
        let item = this.datum[this.selectRow];
        keys.forEach(key => {
          parametricParam.push({
            name: key,
            expression: `${item[key]}`
          })
        });

        //根据输入的参数进行参数化模型插入
        let params = {
          templateParam:parametricParam,
          modalParam:{
            knowledgeId: this.templateParams.id,
            templateId: this.templateParams.param.templateId,
            insertPos: new THREE.Vector3(),
            parametricTableId: this.templateParams.param.parametricTableId
          },
          elementItem:curElementTemplate,
        }
        elementLibraryCmd.executeInsertLibPartCommand(params,result =>{
          //this.close();
        })
      },
    },
    mounted() {

    },
    created(){
      elementLibraryCmd.init(500, 200, 500, () => {
        let params = {
          cmdName: 'getTransmitData',
        }
        //通过产品的全局变量来存储不相关组件中的数据并实现互传
        elementLibraryCmd.executeElementLibraryCommand(params, res => {
          this.templateParams = res;
          let paramsGetParameters = {
            cmdName: 'getElementModelParametricTable',
            id: res.param.parametricTableId,
          }
          elementLibraryCmd.executeElementLibraryCommand(paramsGetParameters,result => {
            if (result.status === 200) {
              this.fillData(result.data.data?.parameters);
            }
          })
        })
      });
    },
    computed: {
      // refPictureBtnText() {
      //   return this.showRefPicture ? this.$t('message.KnowledgeBase.hiddenRefPicture') : this.$t('message.KnowledgeBase.showRefPicture');
      // },
      // documentId() {
      //   return knowledgeBaseManager.getItemById(this.modalParams.knowledgeId).param.documentId;
      // }
    }
  }
</script>

<style scoped>
  .wrap {
    font-size: 12px;
  }
</style>